package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.*;
import com.hopu.blb_erp.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;

@Api
@RestController
public class TParController {
    @Autowired
    private TParService tParService;
    @Autowired
    private UserService userService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ISCompanyService companyService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private TRelationService tRelationService;

    @ApiOperation("查询本公司合作关系申请记录")
    @GetMapping("/tPars/page/{current}/{pageSize}")
    public ResponseEntity<PageEntity<TPar>> findPage(@PathVariable("current") Integer current,@PathVariable("pageSize")Integer pageSize,String startTime,String endTime){
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userService.findByName(user.getUsername());
        if ("".equals(startTime)){
            startTime = null;
        }
        if ("".equals(endTime)){
            endTime = null;
        }
        PageEntity<TPar> page = tParService.findAPage(byName.getCompanyId(), current, pageSize,startTime,endTime);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("查询本公司合作关系审核记录")
    @GetMapping("/checks/page/{current}/{pageSize}")
    public ResponseEntity<PageEntity<TPar>> findCheckPage(@PathVariable("current") Integer current,@PathVariable("pageSize")Integer pageSize,String name,String time){
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if ("".equals(name) || "null".equals(name)){
            name = null;
        }
        if ("".equals(time) || "null".equals(time)){
            time = null;
        }
        User byName = userService.findByName(user.getUsername());
        PageEntity<TPar> page = tParService.findBPage(byName.getCompanyId(), current, pageSize,name,time);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("添加申请")
    @PostMapping("/tPar/{comId}/{parRel}")
    public ResponseEntity<String> addTPar(@PathVariable("comId") Integer comId,@PathVariable("parRel") String parRel){
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User)
                        SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User byName = userService.findByName(user.getUsername());
        if (byName.getCompanyId() == comId){
            return ResponseEntity.ok("no");
        }
        //判断客户关系是否存在
        SCompany company = companyService.getById(byName.getCompanyId());
        Customer customer = customerService.getOne(new QueryWrapper<Customer>().eq("name",company.getCompanyName()));
        if ("1".equals(parRel) && customer != null){
            return ResponseEntity.ok("exist");
        }
        //判断供应商关系是否存在
        Supplier supplier = supplierService.getById(company.getId());
        if ("0".equals(parRel) && supplier != null){
            TRelation relation = tRelationService.getOne(new QueryWrapper<TRelation>().eq("supplier_id", byName.getCompanyId()).eq("customer_id", comId));
            if (relation != null){
                return ResponseEntity.ok("exist");
            }
        }
        //判断申请列表是否存在
        TPar one = tParService.getOne(new QueryWrapper<TPar>().eq("t_par_com_a", byName.getCompanyId()).eq("t_par_com_b", comId).eq("t_par_rel", parRel));
        if (one != null){
            return ResponseEntity.ok("exists");
        }
        TPar tPar = new TPar();
        tPar.setTParComA(byName.getCompanyId());
        tPar.setTParComB(comId);
        tPar.setTParStatus("已申请");
        tPar.setTParStatus2("未查看");
        LocalDateTime now = LocalDateTime.now();
        tPar.setTParTime(Timestamp.valueOf(now));
        tPar.setTParRel(parRel);
        tParService.save(tPar);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("拒绝申请")
    @PutMapping("/tPar/no/{no}")
    public ResponseEntity<String> tParNo(@PathVariable("no")String no,@RequestBody TPar tPar){
        tPar.setTParStatus(no);
        tPar.setTParStatus2(no);
        tParService.updateById(tPar);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation("忽略申请")
    @PutMapping("/tParDel/{del}")
    public ResponseEntity<String> tParDel(@PathVariable("del")String del,@RequestBody TPar tPar){
        tPar.setTParStatus2(del);
        tParService.updateById(tPar);
        return ResponseEntity.ok("ok");
    }
}
